package cn.util.data;






import cn.util.exception.BusinessException;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;


public final class StringUtil {
	private StringUtil() {

	}

	/**
	 * 判断字符串为空，包含null与空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || "".equals(str);
	}

	/**
	 * 判断字符串不空，包含null与空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !(str == null || "".equals(str));
	}

	/**
	 * 判断字符串不空，包含null、空字符串与只包含空格,
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmptyAfterTrim(String str) {
		if (str == null || "".equals(str.trim())) {
			return false;
		}
		return true;

	}

	/**
	 * 判断字符串为空，包含null、空字符串与只包含空格,
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmptyAfterTrim(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		}
		return false;

	}

	/**
	 * 去掉字符串的前后空格，将单引号改成两个单引号
	 * 
	 * @param str
	 * @return
	 */
	public static String trimAndEscapeQuote(String str) {
		if (str == null) {
			return str;
		}
		return str.trim().replaceAll("'", "''");
	}

	/**
	 * 按分隔符分隔字符串，并去掉各段的空格
	 * 
	 * @param str
	 * @param split
	 * @return
	 */
	public static String[] splitAndTrim(String str, String split) {
		String[] result = null;
		if (isNotEmpty(split) && isNotEmpty(str)) {
			result = str.split(split);
			if (result != null && result.length > 0) {
				for (int i = 0; i < result.length; i++) {
					result[i] = result[i].trim();
				}
			}

		}
		return result;

	}

	/**
	 * 去掉字符串的null值
	 * 
	 * @param str
	 * @return
	 */
	public static String trimNull(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	/**
	 * 将参数转换成字符串,当参数为null时，返回空字符串
	 * 
	 * @param integer
	 * @return
	 */
	public static String toString(Integer integer) {
		if (integer == null) {
			return "";
		}
		return integer.toString();
	}

	/**
	 * 用相同的值填充字符串
	 * 
	 * @param value
	 * @param num
	 * @return
	 */
	public static String fill(String value, int num) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < num; i++) {
			stringBuilder.append(value);
		}

		return stringBuilder.toString();
	}

	/**
	 * 逗号分隔的字符串转化成Set
	 * 
	 * @param s
	 * @return
	 */
	public static Set<String> commaDelimitedStringToSet(String s) {
		Set<String> set = new HashSet<String>();
		String[] split = s.split(",");
		for (String aSplit : split) {
			String trimmed = aSplit.trim();
			if (trimmed.length() > 0)
				set.add(trimmed);
		}
		return set;
	}

	/**
	 * 字符串达不到指定长度的字符串，右填空格，超过指定长度，截断
	 * 
	 * @param length
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String rightFillSpace(String str, int length) {
		if (isEmpty(str)) {
			return getStr(" ", length);
		}
		if (length < 1) {
			return "";
		}
		String result = "";
		try {
			int oldLength = str.getBytes("gbk").length;			
			if (oldLength == length) {
				result = str;
			} else if (oldLength < length) {
				result = str + getStr(" ", length - oldLength);
			} else {
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < str.length(); i++) {
					sb.append(str.charAt(i));

					if (sb.toString().getBytes("gbk").length >= length) {
						break;
					}

				}
				result = sb.toString();
			}
		} catch (UnsupportedEncodingException e) {
			throw new BusinessException(e.getMessage(),e);
		}
		return result;

	}

	public static String getStr(String str, int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append(str);
		}
		return sb.toString();
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String result = rightFillSpace("我的我的我的我的我1的我的", 20);
		System.out.println(result + ":" + result.getBytes("gbk").length);

	}
}
